package com.hospital.dictionary.module.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.dictionary.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public abstract class BaseController<T,S extends IService<T>> {

    @Autowired
    protected S baseService;

    @GetMapping(value = "/list")
    public Tip queryList(T addressDict, @RequestParam(name = "pageNo",defaultValue = "1")Integer pageNo,
                         @RequestParam(name="pageSize",defaultValue = "10")Integer pageSize, HttpServletRequest req){
        QueryWrapper<T> queryWrapper= QueryGenerator.initQueryWrapper(addressDict,req.getParameterMap());
        Page<T> page=new Page<>(pageNo,pageSize);
        Page<T> pageList= (Page<T>) baseService.page(page,queryWrapper);
        return new SuccessTip(pageList);
    }

    @PostMapping(value = "create")
    public Tip createDo(@RequestBody T object){
        boolean save = baseService.save(object);
        return new SuccessTip(save);
    }

    @PostMapping(value = "update")
    public Tip updateDto(@RequestBody T object){
        boolean result = baseService.updateById(object);
        return new SuccessTip(result);
    }

    @GetMapping(value="detail")
    public Tip detail(){
        return new SuccessTip("牛逼不");
    }


    @GetMapping(value = "/listAll")
    public Tip queryListAll(){
        List<T> list = baseService.list(new QueryWrapper<>());
        return new SuccessTip(list);
    }
}
